package com.mtiiot.fsmanage.utils;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by xyr on 2016/9/27.
 * MD5加密工具类
 */
public class MD5Common {
    private static Object md5Lock = new Object();
    public static MessageDigest md;
    static {
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    /**
     *
     * <p>Title: encryptMD5</p>
     * <p>Description: </p>
     * <p>Company: CVTE</p>
     * @author yinzhichao@cvte.cn
     * @date 2014-5-8
     * @param encTxt
     * @return
     */
    public static String encryptMD5(String encTxt) {
        StringBuffer buf = null;
        byte[] b = null;
        synchronized (md5Lock) {
            md.update(encTxt.getBytes());
            b = md.digest();
        }
        buf = new StringBuffer(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            if (((int) b[i] & 0xff) < 0x10) {
                buf.append("0");
            }
            buf.append(Long.toHexString((int) b[i] & 0xff));
        }
        return buf.toString();
    }

}
